<template>
  <div class="home-view">
    <el-carousel height="400px">
      <el-carousel-item v-for="(image, index) in carouselImages" :key="index">
        <img :src="image" class="carousel-image" />
      </el-carousel-item>
    </el-carousel>

    <!-- 网站公告区域 -->
    <div v-if="notices.length > 0" class="notice-section">
      <div class="notice-header">
        <div class="header-decoration">
          <div class="film-strip">
            <div class="film-hole"></div>
            <div class="film-hole"></div>
            <div class="film-hole"></div>
          </div>
        </div>
        <h3 class="notice-title">
          <el-icon class="title-icon"><Notification /></el-icon>
          <span class="title-text">最新公告</span>
          <div class="title-underline"></div>
        </h3>
        <div class="header-decoration right">
          <div class="film-strip">
            <div class="film-hole"></div>
            <div class="film-hole"></div>
            <div class="film-hole"></div>
          </div>
        </div>
      </div>
      
      <div class="notice-container">
        <div class="notice-list">
          <div 
            v-for="(notice, index) in notices.slice(0, 3)" 
            :key="notice.id" 
            class="notice-item"
            :class="[getNoticeClass(notice.type), { 'expanded': activeNotices.includes(notice.id.toString()) }]"
            @click="toggleNotice(notice.id.toString())"
          >
            <div class="notice-item-header">
              <div class="notice-item-left">
                <div class="notice-badge" :class="`badge-${notice.type}`">
                  <span class="badge-text">{{ getNoticeTypeText(notice.type) }}</span>
                </div>
                <h4 class="notice-item-title">{{ notice.title }}</h4>
              </div>
              <div class="notice-item-right">
                <span class="notice-time">{{ formatTime(notice.created_at) }}</span>
                <el-icon class="expand-icon" :class="{ rotated: activeNotices.includes(notice.id.toString()) }">
                  <ArrowDown />
                </el-icon>
              </div>
            </div>
            
            <div class="notice-item-content" v-show="activeNotices.includes(notice.id.toString())">
              <div class="content-inner">
                <div class="notice-text" v-html="formatNoticeContent(notice.content)"></div>
                <div v-if="notice.end_time" class="notice-deadline">
                  <el-icon><Clock /></el-icon>
                  <span>截止时间：{{ formatTime(notice.end_time) }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <h2>正在热映</h2>
    <div class="movie-grid">
      <MovieCard v-for="movie in allMovies" :key="movie.id" :movie="movie" />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { Notification, ArrowDown, Clock } from '@element-plus/icons-vue'
// P4: 导入 P4 配置的 Axios
import request from '../utils/request'
// P4: 导入 P4 创建的组件
import MovieCard from '../components/MovieCard.vue'

const allMovies = ref([])
const notices = ref([])
const activeNotices = ref([])
const carouselImages = ref([
  '/assets/images/s1.jpg',
  '/assets/images/s2.jpg',
  '/assets/images/s3.jpg'
])

// 获取公告类型对应的标签类型
const getNoticeTagType = (type) => {
  const typeMap = {
    'important': 'danger',
    'event': 'warning',
    'maintenance': 'info',
    'normal': ''
  }
  return typeMap[type] || ''
}

// 获取公告类型文本
const getNoticeTypeText = (type) => {
  const typeMap = {
    'important': '重要',
    'event': '活动',
    'maintenance': '维护',
    'normal': '通知'
  }
  return typeMap[type] || '通知'
}

// 获取公告样式类
const getNoticeClass = (type) => {
  return `notice-${type}`
}

// 格式化时间
const formatTime = (timeStr) => {
  if (!timeStr) return ''
  const date = new Date(timeStr)
  return date.toLocaleDateString('zh-CN', { 
    year: 'numeric', 
    month: '2-digit', 
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

// 格式化公告内容（将换行符转换为HTML）
const formatNoticeContent = (content) => {
  if (!content) return ''
  return content.replace(/\n/g, '<br>')
}

// 切换公告展开状态
const toggleNotice = (noticeId) => {
  const index = activeNotices.value.indexOf(noticeId)
  if (index > -1) {
    activeNotices.value.splice(index, 1)
  } else {
    activeNotices.value.push(noticeId)
  }
}

// P4: 在页面加载时, 通过 P4 的 Axios 请求 P2/P3 的后端 API
onMounted(async () => {
  try {
    // P4: 请求 P2 的 /api/movies 接口
    const movieRes = await request.get('/movies')
    if (movieRes.data.success) {
      allMovies.value = movieRes.data.data
    }

    // 获取公告数据
    const noticeRes = await request.get('/notices')
    if (noticeRes.data.success) {
      notices.value = noticeRes.data.data
      // 默认全部折叠状态
      activeNotices.value = []
    }
  } catch (error) {
    console.error('获取数据失败:', error)
  }
})
</script>

<style scoped>
.home-view {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.home-view h2 {
  text-align: center;
  margin: 30px 0 20px 0;
  color: #333;
  font-size: 28px;
}
.carousel-image {
  width: 100%;
  height: 100%;
  object-fit: cover; /* 保持图片比例, 裁剪多余部分 */
}
.movie-grid {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  gap: 20px;
  padding: 20px 0;
  max-width: fit-content;
  margin: 0 auto;
}

/* 公告区域样式 - 猫眼电影风格 */
.notice-section {
  margin: 40px 0;
  padding: 0;
}

/* 公告头部设计 */
.notice-header {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 25px;
  position: relative;
}

.header-decoration {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
}

.header-decoration.right {
  transform: rotate(180deg);
}

.film-strip {
  width: 120px;
  height: 20px;
  background: linear-gradient(135deg, #2c3e50, #34495e);
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: space-around;
  box-shadow: 0 2px 8px rgba(44, 62, 80, 0.3);
  position: relative;
}

.film-strip::before {
  content: '';
  position: absolute;
  top: -2px;
  left: -2px;
  right: -2px;
  bottom: -2px;
  background: linear-gradient(45deg, #f39c12, #e74c3c);
  border-radius: 6px;
  z-index: -1;
}

.film-hole {
  width: 6px;
  height: 6px;
  background: #fff;
  border-radius: 50%;
  box-shadow: inset 0 1px 2px rgba(0,0,0,0.2);
}

.notice-title {
  margin: 0 30px;
  text-align: center;
  position: relative;
  white-space: nowrap;
}

.title-icon {
  color: #e74c3c;
  font-size: 24px;
  margin-right: 8px;
  animation: pulse 2s infinite;
}

.title-text {
  font-size: 22px;
  font-weight: 700;
  color: #2c3e50;
  text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
  letter-spacing: 1px;
}

.title-underline {
  position: absolute;
  bottom: -5px;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 3px;
  background: linear-gradient(45deg, #e74c3c, #f39c12);
  border-radius: 2px;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}

/* 公告容器 */
.notice-container {
  background: linear-gradient(135deg, #fff 0%, #f8f9fa 100%);
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  border: 1px solid rgba(231, 76, 60, 0.1);
}

.notice-list {
  display: flex;
  flex-direction: column;
}

/* 公告项目 */
.notice-item {
  cursor: pointer;
  transition: all 0.3s ease;
  border-bottom: 1px solid #eee;
  position: relative;
}

.notice-item:last-child {
  border-bottom: none;
}

.notice-item:hover {
  background: linear-gradient(135deg, #fff5f5 0%, #fefefe 100%);
  transform: translateY(-1px);
  box-shadow: 0 4px 20px rgba(231, 76, 60, 0.1);
}

.notice-item-header {
  padding: 20px 25px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.notice-item-left {
  display: flex;
  align-items: center;
  flex: 1;
}

.notice-badge {
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-right: 15px;
  flex-shrink: 0;
}

.badge-important {
  background: linear-gradient(135deg, #e74c3c, #c0392b);
  color: white;
  box-shadow: 0 2px 8px rgba(231, 76, 60, 0.3);
}

.badge-event {
  background: linear-gradient(135deg, #f39c12, #e67e22);
  color: white;
  box-shadow: 0 2px 8px rgba(243, 156, 18, 0.3);
}

.badge-maintenance {
  background: linear-gradient(135deg, #3498db, #2980b9);
  color: white;
  box-shadow: 0 2px 8px rgba(52, 152, 219, 0.3);
}

.badge-normal {
  background: linear-gradient(135deg, #27ae60, #229954);
  color: white;
  box-shadow: 0 2px 8px rgba(39, 174, 96, 0.3);
}

.notice-item-title {
  font-size: 16px;
  font-weight: 600;
  color: #2c3e50;
  margin: 0;
  line-height: 1.4;
}

.notice-item-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.notice-time {
  color: #7f8c8d;
  font-size: 12px;
  font-weight: 500;
}

.expand-icon {
  font-size: 16px;
  color: #7f8c8d;
  transition: transform 0.3s ease;
}

.expand-icon.rotated {
  transform: rotate(180deg);
}

/* 公告内容 */
.notice-item-content {
  overflow: hidden;
  transition: max-height 0.3s ease;
}

.content-inner {
  padding: 0 25px 20px 25px;
  border-top: 1px solid #f0f0f0;
  background: linear-gradient(135deg, #fafbfc 0%, #ffffff 100%);
}

.notice-text {
  line-height: 1.6;
  color: #555;
  font-size: 14px;
  margin: 15px 0;
  padding: 15px;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 8px;
  border-left: 4px solid #e74c3c;
}

.notice-deadline {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #e74c3c;
  font-size: 13px;
  font-weight: 600;
  padding: 10px 15px;
  background: linear-gradient(135deg, #fff5f5, #ffeaea);
  border-radius: 8px;
  border: 1px solid rgba(231, 76, 60, 0.2);
  margin-top: 10px;
}

.notice-deadline .el-icon {
  font-size: 14px;
}

/* 不同类型公告的左侧装饰条 */
.notice-item::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 4px;
  transition: width 0.3s ease;
}

.notice-important::before {
  background: linear-gradient(135deg, #e74c3c, #c0392b);
}

.notice-event::before {
  background: linear-gradient(135deg, #f39c12, #e67e22);
}

.notice-maintenance::before {
  background: linear-gradient(135deg, #3498db, #2980b9);
}

.notice-normal::before {
  background: linear-gradient(135deg, #27ae60, #229954);
}

.notice-item:hover::before {
  width: 6px;
}

.notice-item.expanded::before {
  width: 6px;
}
</style>